<?php

class AuthController extends Controller {

    public $layout = 'application.views.layouts.main';
    public $defaultAction = 'login';

    public function actionLogin() {

        $model=new ULoginForm();
        // collect user input data
        if(isset($_POST['ULoginForm'])) {
            $model->attributes=$_POST['ULoginForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate())
                $this->redirect(Yii::app()->user->returnUrl);
        }
        // display the login form
        $this->render('login',array('model'=>$model));

    }

    public function actionLogout() {
        Yii::app()->user->logout();
        $this->redirect('/');
    }

    public function actionRegister() {

    //if user is logged in, logout
        if(!Yii::app()->user->isGuest) Yii::app()->user->logout();

        $model=new UUserForm('register');
        // collect user input data
        if(isset($_POST['UUserForm'])) {
            $model->attributes=$_POST['UUserForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate()) {
                $User = new Users();
                $User->setAttributes($model->attributes);
                $User->password = UrbacHelper::hashWord($User->password);
                $User->valid_code = UrbacHelper::hashWord($User->username.$User->email, true);
                if($this->sendActivationMail($User)) {
                    $User->insert();

                    Yii::app()->authManager->assign($this->module->defaultRole, $User->id);
                    $this->render('message', array('message'=>'Dziękujemy.<br>Na adres email wysłany został kod aktywacyjny.'));
                    return;
                }
                else {
                    throw new CHttpException(404,'Nie można wysłać maila aktywacyjnego.');
                }
            //@TODO: assign auth

            //                $this->redirect(Yii::app()->user->returnUrl);
            }
        }
        // display the login form
        $this->render('register',array('model'=>$model));

    }

    public function actionActivation() {

        $User = Users::model()->findByAttributes(array(
            'username'=>$_GET['username'],
            'valid_code'=>$_GET['code'],
        ));

        if($User === null)
            $this->render('message', array('message'=>'Konto nie zosało aktywowane. Kod aktywacyjny jest niepoprawny.'));
        else {
            if( $User->active == 1 )
                $this->render('message', array('message'=>'Konto jest już aktywne.'));
            else {
                $User->active = 1;
                $User->update();

                $this->render('message', array('message'=>'Konto zosało aktywowane.'));
            }
        }
    }

    public function actionUpdate() {

        $model=new UUserForm('update');
        // collect user input data
        if(isset($_POST['UUserForm'])) {
            $model->attributes=$_POST['UUserForm'];
            // validate user input and redirect to the previous page if valid
            if($model->validate()) {
                $User = Users::model()->findByPk(Yii::app()->user->id);
                $User->password = UrbacHelper::hashWord($model->password);
                if($User->update()) {
                    $this->render('message', array('message'=>'Profil został zaktualizowany.'));
                    return;
                }
                else {
                    throw new CHttpException(404,'Nie można zaktualizować profilu.');
                }
            }
        }
        $this->render('update', array('model'=>$model));
    }

    public function sendActivationMail(Users $User) {

        $module = $this->module;

        $body = $this->renderPartial('/mail/confirmationEmail', array('user'=>$User, 'code'=>$code), true, true);

        $mailer = Yii::app()->mailer;
        $mailer->From = $module->emailFrom;
        $mailer->AddAddress($User->email);
        $mailer->FromName = $module->emailFrom;
        $mailer->CharSet = 'UTF-8';
        $mailer->isHTML();
        $mailer->Subject = 'Potwierdzenie rejestracji na downloadNow.pl';
        $mailer->Body = $body;
        return $mailer->Send();
    }


}